/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xuidemo.slice.banner;

import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.RecycleItemProvider;
import ohos.app.Context;

import com.xuexiang.xui_lib.component.banner.BannerLayout;
import com.xuexiang.xuidemo.ResourceTable;

import java.util.ArrayList;

/**
 * @since 2021-04-21
 */
public class RecyclerViewBannerAdapter extends RecycleItemProvider {
    private ArrayList<Integer> dataList;

    public RecyclerViewBannerAdapter setOnBannerItemClickListener(BannerLayout.OnBannerItemClickListener onBannerItemClickListener) {
        return this;
    }

    public RecyclerViewBannerAdapter(AbilitySlice mSlice, ArrayList<Integer> list) {
        this.dataList = list;
    }


    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int i) {
        return dataList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        Context context = componentContainer.getContext();
        ViewHolder viewHolder;
        final Component cpt;
        if (component == null) {
            viewHolder = new ViewHolder();
            cpt = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_banner_recycler_view_item, null, false);
            viewHolder.image = (Image) cpt.findComponentById(ResourceTable.Id_image_item);
            cpt.setTag(viewHolder);
        } else {
            cpt = component;
            viewHolder = (ViewHolder) cpt.getTag();
        }
        viewHolder.image.setPixelMap(dataList.get(i));
        viewHolder.image.setScaleMode(Image.ScaleMode.CENTER);
        AnimatorProperty property = viewHolder.image.createAnimatorProperty();
        property.moveFromX(50).moveToX(1000).rotate(90).alpha(0).setDuration(2500).setDelay(500);
        return cpt;
    }

    public static class ViewHolder {
        public Image image;
    }
}
